import 'package:flutter/material.dart';
import 'package:flutter_trip/widgets/common_image.dart';

class SearchBar extends StatefulWidget {
  final bool shwoLocation;
  final Function goBackCallback;
  final String inputValue;
  final String defaultInputValue;
  final Function onCancel;
  final bool showMap;
  final Function onSearch;
  final ValueChanged<String> onSearchSubmit;

  const SearchBar({ Key key, this.shwoLocation, this.goBackCallback, this.inputValue, this.defaultInputValue = '请输入关键词', this.onCancel, this.showMap, this.onSearch, this.onSearchSubmit }) : super(key: key);

  @override
  _SearchBarState createState() => _SearchBarState();
}

class _SearchBarState extends State<SearchBar> {
  String _searchWord = '';
  TextEditingController _controller;
  FocusNode _focus;

  Function _onClean() {
    _controller.clear();
    setState(() {
      _searchWord = '';
    });
  }

  @override
  void initState() {
    _focus = FocusNode();
    _controller = TextEditingController(text: widget.inputValue);
    super.initState();
  }
  
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        children: [
          if(widget.shwoLocation!=null)
            Padding(
              padding: EdgeInsets.only(right: 10.0),
              child: GestureDetector(
                onTap: (){},
                child: Row(
                  children: [
                    Icon(Icons.room, color: Colors.green, size: 16.0,),
                    Text('苏州', style: TextStyle(color: Colors.black, fontSize: 14.0),)
                  ],
                ),
              ),
            ),
          if(widget.goBackCallback!=null)
            Padding(
              padding: EdgeInsets.only(right: 10.0),
              child: GestureDetector(
                onTap: widget.goBackCallback,
                child: Icon(Icons.chevron_left, color: Colors.black87,),
              ),
            ),
          Expanded(
            child: Container(
              height: 30.0,
              padding: EdgeInsets.only(right: 10.0),
              decoration: BoxDecoration(
                color: Colors.grey[200],
                borderRadius: BorderRadius.circular(18)
              ),
              child: TextField(
                focusNode: _focus,
                textInputAction: TextInputAction.search,
                controller: _controller,
                style: TextStyle(fontSize: 14.0),
                onChanged: (String value){
                  setState(() {
                    _searchWord = value;
                  });
                },
                onTap: (){
                  if(widget.onSearchSubmit!=null){
                    _focus.unfocus();
                  }
                  if(widget.onSearch!=null){
                    widget.onSearch();
                  }
                },
                onSubmitted: widget.onSearchSubmit,
                textAlign: TextAlign.start,
                decoration: InputDecoration(
                  contentPadding: EdgeInsets.only(top: -4),
                  hintText: widget.defaultInputValue,
                  hintStyle: TextStyle(
                    color: Colors.grey,
                    fontSize: 14,
                  ),
                  border: InputBorder.none,
                  prefixIcon: Icon(
                    Icons.search,
                    size: 24,
                    color: Colors.grey,
                  ),
                  suffixIcon: GestureDetector(
                    onTap: () {
                      _onClean();
                    },
                    child: Icon(
                      Icons.clear,
                      size: 24,
                      color: _searchWord=='' ? Colors.grey[200] : Colors.grey,
                    ),
                  )
                ),
              ),
            ),
          ),
          SizedBox(width: 4.0),
          if(widget.onCancel!=null)
            Padding(
              padding: EdgeInsets.only(right: 10.0),
              child: GestureDetector(
                onTap: widget.onCancel,
                child: Text(
                  '取消',
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 14.0,
                  ),
                ),
              ),
            ),
          if(widget.showMap!=null)
            CommonImage('static/icons/widget_search_bar_map.png'),
        ],
      ),
    );
  }
}